<?php

class NewsController extends Controller
{
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations

		);
	}
	public function allowedActions()
	{
		return '*';
	}
	public function actionIndex($alias = null)
	{
		$category = ActicleCategories::model()->findByAttributes(array('alias'=>$alias));
		if($category === null)
			throw new CHttpException('404','Không tìm thấy trang.');
		$criteria = new CDbCriteria();
		$criteria->with = array('ActicleCategories');
		$criteria->order = "t.id DESC";
		$criteria->condition = "ActicleCategories.alias=:alias";
		$criteria->params = array('alias' => $alias);
		
		$count=Acticles::model()->count($criteria);
		$pages=new CPagination($count);

		// results per page
		$pages->pageSize=4;
		$pages->applyLimit($criteria);
		
		$acticles = Acticles::model()->findAll($criteria);
		$products = Products::model()->findAll(array('order'=>'rand()','limit'=>3));
		$this->pageTitle = $category->name;
		$this->render('index',array('acticles'=>$acticles,'category'=>$category,'pages'=>$pages,'products'=>$products));
	}
	public function actionDetail($alias = null){
		$model = Acticles::model()->findByAttributes(array('alias'=>$alias));
		if($model === null)
			throw new CHttpException('404','Không tìm thấy trang.');
		$this->pageTitle = $model->name;
		$this->image = Yii::app()->baseUrl.'/upload/image.php?width=200&height=200&image='.Yii::app()->baseUrl.'/upload/acticles/'.$model->image;
		$this->render('detail',array('model'=>$model));
	}
}